package com.shiku.imserver.repository;

import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.shiku.imserver.common.IService;
import com.shiku.imserver.service.IMBeanUtils;
import java.util.ArrayList;
import java.util.List;
import org.bson.Document;

public class GroupRepository extends BaseRepository implements IService {
    private static final String ROOMJID_LIST = "shiku_roomJids_userId";
    private MongoDatabase userJidListDB;

    public boolean initialize() {
        IMBeanUtils.getImconfig().getMongoConfig();
        try {
            this.userJidListDB = IMBeanUtils.getBeanManager().getMongoClient().getDatabase(ROOMJID_LIST);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    public List<String> queryUserRoomsJidList(int userId) {
        List<String> jids = new ArrayList<>();
        MongoCursor<String> iterator = getCollection(this.userJidListDB, (long) userId).distinct("jid", new Document("userId", Integer.valueOf(userId)), String.class).iterator();
        while (iterator.hasNext()) {
            try {
                jids.add((String) iterator.next());
            } finally {
                if (iterator != null) {
                    iterator.close();
                }
            }
        }
        return jids;
    }
}
